﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Space_Conquest
{
    class ChatBox : TextBox
    {

        #region Private Members

        GameEngine _parent;

        #endregion

        #region Properties

        public GameEngine Parent
        {
            get
            {
                return _parent;
            }
        }

        #endregion

        #region Constructor

        public ChatBox(GameEngine parent, int x, int y) :
            base(x, y)
        {
            _parent = parent;
        }

        #endregion

        #region Methods

        public override void Render()
        {
            if (_isSelected)
            {
                base.Render();
            }
        }

        public override bool MouseDown(System.Windows.Forms.MouseButtons button, int x, int y)
        {

            // Don't want to capture this anymore, override to do nothing

            return false;
        }

        public override bool KeyDown(System.Windows.Forms.Keys key, bool shift)
        {

            // Override _isSelected for ENTER instead of MouseDown

            if(_isSelected)
            {
                if (key == Keys.Return)
                {
                    // Send to Chat Manager
                    if (_text != "")
                    {
                        if (!ParseCommand(_text))
                        {
                            Parent.RedirectGameEvent(new ChatEvent(Parent.LocalPlayer, _text));
                        }
                        _text = "";
                    }
                    _isSelected = false;
                }
                else
                {
                    _text = Helpers.ParseKey(_text, key, shift);
                }

                return true;
            }
            else
            {
                if (key == Keys.Return)
                {
                    _isSelected = true;
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }

        bool ParseCommand(string text)
        {

            if ((!Parent.IsMuliplayer) && text.StartsWith("/name "))
            {
                text = text.Replace("/name ", "");
                Parent.LocalPlayer.Name = text;
                return true;
            }

            #if DEBUG
            else if (text.StartsWith("/set "))
            {
                if (!Parent.IsMuliplayer)
                {
                    text = text.Replace("/set ", "");
                    if (text.StartsWith("metal "))
                    {
                        text = text.Replace("metal ", "");
                        Parent.LocalPlayer.Metal = Convert.ToInt32(text);
                        return true;
                    }
                    else if (text.StartsWith("fuel "))
                    {
                        text = text.Replace("fuel ", "");
                        Parent.LocalPlayer.Fuel = Convert.ToInt32(text);
                        return true;
                    }
                    else if (text.StartsWith("oil "))
                    {
                        text = text.Replace("oil ", "");
                        Parent.LocalPlayer.Fuel = Convert.ToInt32(text);
                        return true;
                    }
                    else if (text.StartsWith("electricity "))
                    {
                        text = text.Replace("electricity ", "");
                        Parent.LocalPlayer.Electricity = Convert.ToInt32(text);
                        return true;
                    }
                    else if (text.StartsWith("all "))
                    {
                        text = text.Replace("all ", "");
                        Parent.LocalPlayer.ExploreEntireMap();
                        return true;
                    }
                    else if (text.StartsWith("map"))
                    {
                        Parent.LocalPlayer.ExploreEntireMap();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }

            }
            #endif

            else
            {
                return false;
            }

        }

        #endregion

    }
}
